/**
 * @{#} BaseActivity.java Create on 2014年10月28日 上午10:37:04    
 *
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0
 * @description
 *
 */
package com.common.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.common.R;
import com.common.event.LoginOutEvent;
import com.common.event.NetWorkChangeEvent;
import com.common.utility.CommonUtility;
import com.common.utility.Logger;
import com.common.widget.ActionBar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import de.greenrobot.event.EventBus;

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {

    public Activity activity;

    public ActionBar mActionBar;
    /** 从相机拍照 **/
    protected static final int PHOTO_TAKE_DATA = 0x10;
    /** 从媒体库选择图片 */
    protected static final int PHOTO_PICKED_WITH_DATA = 0x11;
    /** 剪切图片 */
    protected static final int PHOTO_CROP_DATA = 0x12;

    protected ImageLoader mImageLoader;

    //有可能为null值,网络状态
    private NetWorkChangeEvent netWorkChangeEvent;

    /*
     * (non-Javadoc)
     *
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        activity = this;
        EventBus.getDefault().register(this);
        mImageLoader = ImageLoader.getInstance();
    }
    /*
     * (non-Javadoc)
     *
     * @see android.support.v4.app.FragmentActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    public void initActionBar() {
        mActionBar = (ActionBar) findViewById(R.id.action_bar);
        //处理actionbar的左右点击事件
        if(mActionBar != null){
            mActionBar.getNavLeftLayout().setOnClickListener(this);
            mActionBar.getNavRightLayout().setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ll_nav_left) {
            finish();
        }
    }

    public abstract void onEvent(LoginOutEvent event);

    public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    /**
     * 子类可以重写该方法,获取对网络改变的通知 -- 不建议使用,可以用下面的onNetWorkChange方法
     * {@link com.common.activity.BaseActivity onNetWorkChange}
     * @param event
     */
    @Deprecated
    public void onEventMainThread(NetWorkChangeEvent event){
        this.netWorkChangeEvent = event;
        CommonUtility.UIUtility.toast(activity,"网络连接"+(event.isConnect ? "成功":"失败")+"!");
        Logger.e("mark","isConnect:"+event.isConnect+";type:"+event.netType);

        onNetWorkChange(event.isConnect,event.netType);
    }
    //子类可以重写该方法,获取对网络改变的通知
    public void onNetWorkChange(boolean isConnect,int type){

    }


}
